﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Odbc;
using System.Collections.Specialized;

namespace ManagmentWMS
{
    class Firebird
    {
        protected  OdbcConnection connection;
        private OdbcConnectionStringBuilder conStringBuilder;

        public Firebird()
        {
            try
            {
                conStringBuilder = new OdbcConnectionStringBuilder();
                conStringBuilder = new OdbcConnectionStringBuilder();
                conStringBuilder.Driver = FirebirdConfig.driver;
                conStringBuilder.Add("DBNAME", FirebirdConfig.server + ":" + FirebirdConfig.database);
                //conStringBuilder.Add("Dbname", FirebirdConfig.database);
                conStringBuilder.Add("CHARSET", FirebirdConfig.charset);
                conStringBuilder.Add("ROLE", FirebirdConfig.role);
                conStringBuilder.Add("UID", "");
                conStringBuilder.Add("PWD", "");
                connection = new OdbcConnection(conStringBuilder.ConnectionString);
            }
            catch (Exception ex)
            {
                throw new Exception("Błąd logowania", ex);
            }
        }

        private void connectionOpen()
        {
            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection = new OdbcConnection(conStringBuilder.ConnectionString);
                    connection.Open();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Błąd otwarcia bazy danych.", ex);
            }            
        }

        private void connectionClose()
        {            
            try
            {
                connection.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Błąd rozłączenia z bazą danych", ex);
            }
        }

        public void Login(string login, string passwd)
        {            
            try
            {
                conStringBuilder["Uid"] = login.ToUpper();
                conStringBuilder["Pwd"] = passwd;
                connectionOpen();
            }
            catch (Exception ex)
            {
                throw new Exception("Błąd logowania.",ex);
                //do logów
            }
        }

        public void Logout()
        {            
            try
            {
                connectionClose();
            }
            catch (Exception ex)
            {
                throw new Exception("Błąd przy wylogowywaniu.",ex);
                //do logów
            }
        }

        public StringDictionary GetDictionary(string query)
        {
            StringDictionary dictionary = new StringDictionary();
            OdbcCommand command = new OdbcCommand(query, connection);
            OdbcDataReader dr = command.ExecuteReader();
            while (dr.Read())
            {
                dictionary.Add(dr[0].ToString(),dr[1].ToString());
            }
            return dictionary;
        }

        public DataSet GetData(string query, string name)
        {
            OdbcCommand command = new OdbcCommand(query, connection);
            OdbcDataAdapter dataAdapter = new OdbcDataAdapter(command);
            DataSet ds = new DataSet(name);
            dataAdapter.Fill(ds);
            return ds;
        }
    }
}
